package com.sfac.java_spring_boot_02.modules.test.controller;


import com.sfac.java_spring_boot_02.modules.common.entity.Result;
import com.sfac.java_spring_boot_02.modules.common.entity.SearchBean;
import com.sfac.java_spring_boot_02.modules.test.entity.Student;
import com.sfac.java_spring_boot_02.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description StudentController
 * @Author HymanHu
 * @Date 2021/9/14 15:01
 */
@RestController
@RequestMapping("/api")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 127.0.0.1/api/student ---- post
     * {"studentName":"hujiang","age":18,"email":"hujiang@163.com"}
     * {"studentName":"hujiang1","age":19,"email":"hujiang1@163.com","card":{"cardNo":"001"}}
     */
    @PostMapping(value = "/student", consumes = "application/json")
    public Result<Student> insertStudent(@RequestBody Student student) {
        return studentService.insertStudent(student);
    }

    /**
     * 127.0.0.1/api/student ---- put
     * {"id":1,"studentName":"hujiangaa","age":20,"email":"hujiangaa@163.com"}
     */
    @PutMapping(value = "/student", consumes = "application/json")
    public Result<Student> updateStudent(@RequestBody Student student) {
        return studentService.updateStudent(student);
    }

    /**
     * 127.0.0.1/api/student/2 ---- delete
     */
    @DeleteMapping("/student/{studentId}")
    public Result<Object> deleteStudentByStudentId(@PathVariable int studentId) {
        return studentService.deleteStudentByStudentId(studentId);
    }
    /**
     * 127.0.0.1/api/student?studentName=hujiang2&email=hujiang2@163.com ---- get
     */
    @GetMapping("/student")
    public Student findTopByStudentNameAndEmail(@RequestParam String studentName,
                                                @RequestParam String email) {
        return studentService.findTopByStudentNameAndEmail(studentName, email);
    }
    /**
     * 127.0.0.1/api/students ---- post
     * {"currentPage":1,"pageSize":5,"orderBy":"email","direction":"desc","keyword":""}
     */
    @PostMapping(value = "/students", consumes = "application/json")
    public Page<Student> getStudentsBySearchBean(@RequestBody SearchBean searchBean) {
        return studentService.getStudentsBySearchBean(searchBean);
    }

    /**
     * 127.0.0.1/api/student/1 ---- get
     */
    @GetMapping("/student/{studentId}")
    public Student getStudentByStudentId(@PathVariable int studentId) {
        return studentService.getStudentByStudentId(studentId);
    }
    /**
     * 127.0.0.1/api/students?studentName=hu&email=hj ---- get
     */
    @GetMapping("/students")
    public List<Student> getStudentsByStudentNameOrEmail(
            @RequestParam String studentName,
            @RequestParam String email) {
        return studentService.getStudentsByStudentNameOrEmail(
                String.format("%s%s%s","%", studentName, "%"),
                String.format("%s%s%s","%", email, "%"));
    }
}
